package com.glq1218.util;

import com.glq1218.domain.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author glq1218
 */
public class SecurityUtils {
    public static SecurityContext getSecurityContext() {
        return SecurityContextHolder.getContext();
    }

    public static Authentication getAuthentication() {
        return getSecurityContext().getAuthentication();
    }

    /**
     * 获取登录用户
     *
     * @return 登录用户
     */
    public static LoginUser getLoginUser() {
        Object principal = getAuthentication().getPrincipal();
        return (LoginUser) getAuthentication().getPrincipal();
    }

    public static Long getUserId() {
        return getLoginUser().getId();
    }
}
